package com.course.api.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.api.config.Result.Result;
import com.course.api.config.Result.Results;
import com.course.api.entity.Menu;
import com.course.api.entity.User;
import com.course.api.mapper.RoleMenuMapper;
import com.course.api.service.Impl.MenuServiceImpl;
import com.course.api.service.Impl.RoleServiceImpl;
import com.course.api.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ResponseBody
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    MenuServiceImpl menuService;

    /**
     * 用户登录
     * @param user
     * @return map
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        String account = user.getAccount();
        String password = user.getPassword();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        User userVo = userService.getOne(queryWrapper);
        // 登陆失败
        if (userVo == null) {
            return Results.failure("不存在此用户");
        } else if (!userVo.getPassword().equals(password)) {
            return Results.failure("账号密码输入错误");
        }
        //登陆成功
        StpUtil.login(userVo.getId());
        String token = StpUtil.getTokenValueByLoginId(userVo.getId());
        map.put("userData", userVo);
        map.put("token", token);
        return Results.success(map);
    }

    /**
     * 退出登录
     * @param id
     * @return
     */
    @PostMapping("/logout/{id}")
    public Result logout(@PathVariable(value = "id") Long id) {
        StpUtil.logout(id);
        return Results.success();
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/getAllUserInfo")
    public Result getAllUserInfo() {
        return Results.success(userService.list());
    }

    /**
     * 获取用户列表
     * @param page
     * @param pageSize
     * @param username
     * @param userType
     * @return
     */
    @GetMapping("/getUserList")
    public Result userList(@RequestParam int page,
                           @RequestParam int pageSize,
                           @RequestParam(defaultValue = "") String username,
                           @RequestParam(defaultValue = "") String userType) {
        IPage<User> userPage = new Page<>(page, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", username);
        queryWrapper.like("user_type", userType);
        return Results.success(userService.Page(userPage, queryWrapper));
    }

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        if (userService.save(user)) return Results.success();
        else return Results.failure("添加失败");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        if (userService.updateById(user)) return Results.success();
        else return Results.failure("修改失败");
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result remove(@PathVariable("id") Long id) {
        if (userService.removeById(id)) return Results.success();
        else return Results.failure("删除失败");
    }

    /**
     * 获取教师信息列表
     * @return
     */
    @GetMapping("/getTeacherList")
    public Result getTeacherList() {
        return Results.success(userService.selectTeacherList("teacher"));
    }

    /**
     * 选课
     * @param courseId
     * @param userId
     * @return
     */
    @PutMapping("/addCourse/{courseId}/{userId}")
    public Result addCourse(@PathVariable(value = "courseId") Long courseId,
                            @PathVariable(value = "userId") Long userId) {
        return Results.success(userService.selectCourse(courseId, userId));
    }

    /**
     * 退选
     * @param courseId
     * @param userId
     * @return
     */
    @PutMapping("/deselect/{courseId}/{userId}")
    public Result deselect(@PathVariable(value = "courseId") Long courseId,
                           @PathVariable(value = "userId") Long userId) {
        return Results.success(userService.deselectCourse(courseId, userId));
    }
}
